const baseUrl = "http://127.0.0.1:8080/"

let header = {
  'Accept': 'application/json',
  'content-type': 'application/json; charset=UTF-8',
}

function request(url, params) {
  let token = wx.getStorageSync('token');
  header.token = token
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${baseUrl}${url}`,
      method: params.method,
      data: params.method.toUpperCase() === 'GET' ? params.data : JSON.stringify(params.data),
      header: header,
      success(request) {
        if (request.statusCode === 200) {
          if(request.data.code === "200"){
            resolve(request.data.data)
          }else{
            //reject(request.data.resultVo.message)
            wx.showToast({
              title: request.data.resultVo.message,
              icon:'none'
            })
          }
        } else {
          reject(request.errMsg)
          wx.showToast({
            title: request.errMsg,
            icon:'none'
          })
        }
      },
      fail(error) {
        reject(error.errMsg)
        wx.showToast({
          title: error.errMsg,
          icon:'none'
        })
      }
    })
  })
}

function get(url, params) {
  return request(url, {
    method: 'GET',
    data: params
  })
}

function post(url, params) {
  return request(url, {
    method: 'POST',
    data: params
  })
}

function getBaseUrl(){
  return baseUrl
}

module.exports = {
  get,
  post,
  getBaseUrl
}